package cn.tedu.serviceEdu.controller.front;

import cn.tedu.commonUtils.R;
import cn.tedu.serviceEdu.entity.EduChapter;
import cn.tedu.serviceEdu.entity.EduCourse;
import cn.tedu.serviceEdu.query.FrontCourseQueryVo;
import cn.tedu.serviceEdu.query.FrontCourseWebVo;
import cn.tedu.serviceEdu.service.EduChapterService;
import cn.tedu.serviceEdu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/frontcourse")
@CrossOrigin
public class FrontCourseController {
    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduChapterService chapterService;

    @ApiOperation(value = "分页课程列表")
    @PostMapping(value = "/{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody(required = false) FrontCourseQueryVo courseQuery){
        Page<EduCourse> pageParam = new Page<>(page, limit);
        Map<String, Object> map = courseService.pageListWeb(pageParam, courseQuery);
        return  R.ok().data(map);
    }



    @ApiOperation(value = "根据ID查询课程")
    @GetMapping(value = "/{courseId}")
    public R getById(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId){

        //查询课程信息和讲师信息
        FrontCourseWebVo courseWebVo = courseService.selectInfoWebById(courseId);

        //查询当前课程的章节信息
        List<EduChapter> chapterList = chapterService.getChapterList(courseId);

        return R.ok().data("course", courseWebVo).data("chapterVoList", chapterList);
    }
}
